/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;

using BlindShark.Widgets;

namespace BlindShark.GameStates
{
	/// <summary>
	/// Displays the players, and allows the selection of one of them
	/// </summary>
	public class GS_ConfigurationGame : GameState
	{
	
		#region Variables
		// Previous Gamestate
		GameState _gsPrevious;
		
		// Surfaces
		Surface _sfFrame;
						
		// Texts
		string _sFont;
		
		// Elements location
		Rectangle _rctFrame;
		
		// Buttons
		Button _btnConfigTracklist;
		Button _btnConfigGame;
		Button _btnConfigChallenge;
		Button _btnValidate;
		Button _btnCancel;
		
		// Labels
		Label _lblTitle;
		Label _lblPropositions;
		Label _lblCategories;
		
		// TextBoxes
		TextBox _txtAnswerDelay;
		TextBox _txtGoodAnswer;
		TextBox _txtBadAnswer;
		TextBox _txtListeningDelay;
		
		#endregion
		
		#region Constructors
		public GS_ConfigurationGame(GameState previous)
		{
			_gsPrevious = previous;
			
			// Initializing the fonts used
			_sFont = "fonts/Fertigo_17b.otf";
			Label.DefaultFont = _sFont;
			
			// Initializing the labels
			// Title Label
			_lblTitle = new Label(LanguageManager.Get("CFG_Title"), 36, Color.OrangeRed, TextAlignment.TopMiddle);
			_lblTitle.Underline = true;
			// Propositions Label
			_lblPropositions = new Label("", 20, Color.Orange, TextAlignment.CenterLeft);
			// Categories Label
			_lblCategories = new Label("", 22, Color.Orange, TextAlignment.CenterRight);
			
			// Initializing the buttons
			// Tracklist Tab
			_btnConfigTracklist = new Button("gfx/buttons1.jpg", true);
			_btnConfigTracklist.Click += new Button.ClickEvent(ChangeToTracklist);
			_btnConfigTracklist.Font = _sFont;
			_btnConfigTracklist.TextColor = Color.Orange;
			_btnConfigTracklist.TextSize = 20;
			_btnConfigTracklist.Text = LanguageManager.Get("CF_Tracklist");
			// Game Tab
			_btnConfigGame = new Button("gfx/buttons2.jpg", false);
			_btnConfigGame.Enabled = false;
			_btnConfigGame.Font = _sFont;
			_btnConfigGame.TextColor = Color.SaddleBrown;
			_btnConfigGame.TextSize = 20;
			_btnConfigGame.Text = LanguageManager.Get("CF_Game");
			// Challenge Tab
			_btnConfigChallenge = new Button("gfx/buttons1.jpg", true);
			_btnConfigChallenge.Click += new Button.ClickEvent(ChangeToChallenge);
			_btnConfigChallenge.Font = _sFont;
			_btnConfigChallenge.TextColor = Color.Orange;
			_btnConfigChallenge.TextSize = 20;
			_btnConfigChallenge.Text = LanguageManager.Get("CF_Challenge");
			// Validation Button
			_btnValidate = new Button("gfx/buttons1.jpg", true);
			_btnValidate.Click += new Button.ClickEvent(Validate);
			_btnValidate.Font = _sFont;
			_btnValidate.TextColor = Color.Orange;
			_btnValidate.TextSize = 20;
			_btnValidate.Text = LanguageManager.Get("CF_Validate");
			// Validation Button
			_btnCancel = new Button("gfx/buttons1.jpg", true);
			_btnCancel.Click += new Button.ClickEvent(Cancel);
			_btnCancel.Font = _sFont;
			_btnCancel.TextColor = Color.Orange;
			_btnCancel.TextSize = 20;
			_btnCancel.Text = LanguageManager.Get("CF_Cancel");
						
			// Initializing the TextBoxes
			// Delay for answering
			_txtAnswerDelay = new TextBox();
			_txtAnswerDelay.Size = new Size(32, 26);
			_txtAnswerDelay.FontFile = _sFont;
			_txtAnswerDelay.FontSize = 18;
			_txtAnswerDelay.OnlyNumeric = true;
			_txtAnswerDelay.MaxLength = 3;
			_txtAnswerDelay.Text = GameManager.AnsweringTimeLimit.ToString();
			// Points granted for a good answer
			_txtGoodAnswer = new TextBox();
			_txtGoodAnswer.Size = new Size(32, 26);
			_txtGoodAnswer.FontFile = _sFont;
			_txtGoodAnswer.FontSize = 18;
			_txtGoodAnswer.OnlyNumeric = true;
			_txtGoodAnswer.MaxLength = 3;
			_txtGoodAnswer.Text = GameManager.GainOnGoodAnswer.ToString();
			// Points removed for a bad answer
			_txtBadAnswer = new TextBox();
			_txtBadAnswer.Size = new Size(32, 26);
			_txtBadAnswer.FontFile = _sFont;
			_txtBadAnswer.FontSize = 18;
			_txtBadAnswer.OnlyNumeric = true;
			_txtBadAnswer.MaxLength = 3;
			_txtBadAnswer.Text = GameManager.LossOnBadAnswer.ToString();
			// Listening time
			_txtListeningDelay = new TextBox();
			_txtListeningDelay.Size = new Size(32, 26);
			_txtListeningDelay.FontFile = _sFont;
			_txtListeningDelay.FontSize = 18;
			_txtListeningDelay.OnlyNumeric = true;
			_txtListeningDelay.MaxLength = 3;
			_txtListeningDelay.Text = GameManager.LimitedListening ? GameManager.ListeningTime.ToString() : "0";
		}
		#endregion
		
		#region GameState interface members
		public void Draw()
		{
			// Displaying the Frame
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);
			
			// Displaying the Tab Buttons
			ScreenManager.MainScreen.Blit(_btnConfigTracklist.Surface, _btnConfigTracklist.Location);
			ScreenManager.MainScreen.Blit(_btnConfigGame.Surface, _btnConfigGame.Location);
			ScreenManager.MainScreen.Blit(_btnConfigChallenge.Surface, _btnConfigChallenge.Location);
			ScreenManager.MainScreen.Blit(_btnValidate.Surface, _btnValidate.Location);
			ScreenManager.MainScreen.Blit(_btnCancel.Surface, _btnCancel.Location);
			
			// Displaying the TextBoxes
			ScreenManager.MainScreen.Blit(_txtAnswerDelay.Surface, _txtAnswerDelay.Location);
			ScreenManager.MainScreen.Blit(_txtGoodAnswer.Surface, _txtGoodAnswer.Location);
			ScreenManager.MainScreen.Blit(_txtBadAnswer.Surface, _txtBadAnswer.Location);
			ScreenManager.MainScreen.Blit(_txtListeningDelay.Surface, _txtListeningDelay.Location);
		}
		
		public void CleanUp()
		{
			_sfFrame = null;
			_btnConfigTracklist.CleanUp();
			_btnConfigGame.CleanUp();
			_btnConfigChallenge.CleanUp();
			_btnValidate.CleanUp();
			_btnCancel.CleanUp();
			_txtAnswerDelay.CleanUp();
			_txtGoodAnswer.CleanUp();
			_txtBadAnswer.CleanUp();
			_txtListeningDelay.CleanUp();
			
			Events.Remove();
		}
		
		public void HandleEvents()
		{
			while (Events.Poll())
			{
				// Handling the buttons events
				_btnConfigTracklist.HandleEvents();
				_btnConfigGame.HandleEvents();
				_btnConfigChallenge.HandleEvents();
				_btnValidate.HandleEvents();
				_btnCancel.HandleEvents();
				
				// Handling the Textboxes Events
				_txtAnswerDelay.HandleEvents();
				_txtGoodAnswer.HandleEvents();
				_txtBadAnswer.HandleEvents();
				_txtListeningDelay.HandleEvents();
			}
		}
		
		public void Update()
		{
			
		}
		
		public void Init()
		{
			// Displaying the background one for once
			ScreenManager.MainScreen.Blit(SurfaceManager.GetSurface("gfx/background.png"));
			
			// Initializing the frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(100,50),_sfFrame.Size);
			
			// Initializing the title location
			_lblTitle.Location = new Point(0, 15);
			_lblTitle.Size = _rctFrame.Size;
			_lblTitle.Init();
			_sfFrame.Blit(_lblTitle.Surface, _lblTitle.Location);
			
			// Initializing the buttons
			// Tracklist Tab
			_btnConfigTracklist.Init();
			_btnConfigTracklist.X = _rctFrame.Right + 10;
			_btnConfigTracklist.Y = _rctFrame.Top + 40;
			// Game Tab
			_btnConfigGame.Init();
			_btnConfigGame.X = _rctFrame.Right + 10;
			_btnConfigGame.Y = _btnConfigTracklist.Bottom + 10;
			// Challenge Tab
			_btnConfigChallenge.Init();
			_btnConfigChallenge.X = _rctFrame.Right + 10;
			_btnConfigChallenge.Y = _btnConfigGame.Bottom + 10;
			// Validate Button
			_btnValidate.Init();
			_btnValidate.X = _rctFrame.Right - 10 - _btnValidate.Width;
			_btnValidate.Y = _rctFrame.Bottom - 10 - _btnValidate.Height;
			// Cancel Button
			_btnCancel.Init();
			_btnCancel.X = _rctFrame.Left + 10;
			_btnCancel.Y = _rctFrame.Bottom - 10 - _btnCancel.Height;
			
			// Initializing the categories' Label
			_lblCategories.X = 15;
			_lblCategories.Size = new Size(230, 32);
			// Initializing the propositions' Label
			_lblPropositions.Size = new Size(65, 32);
			
			
			// Category : Answer Delay
			SetCategory(70, LanguageManager.Get("CFG_AnswerDelay") + " :");
			_txtAnswerDelay.Init();
			_txtAnswerDelay.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                     _rctFrame.Y + _lblCategories.Y + 3);
			SetProposition(_txtAnswerDelay.Right - _rctFrame.X + 5, _lblCategories.Y, LanguageManager.Get("CFG_AnswerDelayUnit"));
			
			// Category : Points for a Good Answer
			SetCategory(_lblCategories.Y + _lblCategories.Height * 2 + 10, LanguageManager.Get("CFG_GoodAnswer") + " :");
			_txtGoodAnswer.Init();
			_txtGoodAnswer.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                    _rctFrame.Y + _lblCategories.Y + 3);
			SetProposition(_txtGoodAnswer.Right - _rctFrame.X + 5, _lblCategories.Y, LanguageManager.Get("CFG_GoodAnswerUnit"));
			
			// Category : Points for a Bad Answer
			SetCategory(_lblCategories.Y + _lblCategories.Height, LanguageManager.Get("CFG_BadAnswer") + " :");
			_txtBadAnswer.Init();
			_txtBadAnswer.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                   _rctFrame.Y + _lblCategories.Y + 3);
			SetProposition(_txtBadAnswer.Right - _rctFrame.X + 5, _lblCategories.Y, LanguageManager.Get("CFG_BadAnswerUnit"));
			
			// Category : Listening Time
			SetCategory(_lblCategories.Y + _lblCategories.Height * 2 + 10, LanguageManager.Get("CFG_ListeningDelay") + " :");
			_txtListeningDelay.Init();
			_txtListeningDelay.Location = new Point(_rctFrame.X + _lblCategories.Right + 10,
			                                    _rctFrame.Y + _lblCategories.Y + 3);
			SetProposition(_txtListeningDelay.Right - _rctFrame.X + 5, _lblCategories.Y, LanguageManager.Get("CFG_ListeningDelayUnit"));
		}
		#endregion
	
		#region Buttons Events
		private void SaveConfig()
		{
			GameManager.AnsweringTimeLimit = Convert.ToInt32(_txtAnswerDelay.Text);
			
			GameManager.GainOnGoodAnswer = Convert.ToInt32(_txtGoodAnswer.Text);
			GameManager.LossOnBadAnswer = Convert.ToInt32(_txtBadAnswer.Text);
			
			GameManager.ListeningTime = Convert.ToInt32(_txtListeningDelay.Text);
			GameManager.LimitedListening = GameManager.ListeningTime!=0;
		}
		
		public void ChangeToTracklist()
		{
			SaveConfig();
			GameStateManager.ChangeState(new GS_ConfigurationTracklist(_gsPrevious));
		}
		
		public void ChangeToChallenge()
		{
			SaveConfig();
			GameStateManager.ChangeState(new GS_ConfigurationChallenge(_gsPrevious));
		}
		
		public void Validate()
		{
			SaveConfig();
			// Saving and "jumping" to the previous state
			GameManager.SaveConfiguration();
			GameStateManager.ChangeState(new GS_MenuMain());
		}
		
		public void Cancel()
		{
			// Getting back the old configuration, and "jumping" to the previous state
			GameManager.LoadConfiguration();
			GameStateManager.ChangeState(_gsPrevious);
		}
		#endregion
		
		#region Configuring Labels
		private void SetCategory(int y, string text)
		{
			_lblCategories.Y = y;
			_lblCategories.Text = text;
			_lblCategories.Init();
			_sfFrame.Blit(_lblCategories.Surface, _lblCategories.Location);
		}
		
		private void SetProposition(int x, int y, string text)
		{
			_lblPropositions.Location = new Point(x, y);
			_lblPropositions.Text = text;
			_lblPropositions.Init();
			_sfFrame.Blit(_lblPropositions.Surface, _lblPropositions.Location);
		}
		#endregion
		
	}
}

